﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SDCWeb.DB;
using System.Threading;

namespace SDCWeb.DB
{
    public class DataLoop
    {

        BaseDao basedao = new BaseDao();
        public Thread thread = null;
        string sql = "";

        public void TokenThread()
        {

            
            BaseDao basedao = new BaseDao();
            while (true)
            {
                Random rd1 = new Random();
                int RDNum1 = rd1.Next(-100, 100);
                Thread.Sleep(1000);

                Random rd2 = new Random();
                int RDNum2 = rd2.Next(-100, 100);
                Thread.Sleep(1000);

                Random rd3 = new Random();
                int RDNum3 = rd3.Next(-100, 100);

                //
                double temp100 = 0;     //0.25;
                double temp10 = 0;      //2.5;
                double tempSBZ = 0;
                double tempCQ = 0;
                double tempJSJ = 0;
                

                //处理4T数据
                temp100 =  RDNum1 / 100.0;
                temp10 = RDNum1 / 10.0;

                sql = "update t_d_Device set P=P+(" + temp100.ToString("f2") + "),Q=Q+(" + temp100.ToString("f2") + "),Uab=Uab+(" +(temp100/2).ToString("f2")+"),Iflc=Iflc+("+ temp100.ToString("f2") +"),"+
                    " KD=KD+("+(temp100/5).ToString("f2")+"),ZS=ZS+("+temp10.ToString("f2")+")"+
                    " where DeviceId=2";
                BaseDao.execute(sql);


                //处理1T数据
                temp100 = RDNum2 / 100.0;
                temp10 = RDNum2 / 10.0;

                sql = "update t_d_Device set P=P+(" + temp100.ToString("f2") + "),Q=Q+(" + temp100.ToString("f2") + "),Uab=Uab+(" + (temp100 / 2).ToString("f2") + "),Iflc=Iflc+(" + temp100.ToString("f2") + ")," +
                    " KD=KD+(" + (temp100 / 5).ToString("f2") + "),ZS=ZS+(" + temp10.ToString("f2") + ")" +
                    " where DeviceId=4";
                BaseDao.execute(sql);



                //处理3T数据
                temp100 = RDNum3 / 100.0;
                temp10 = RDNum3 / 10.0;

                sql = "update t_d_Device set P=P+(" + temp100.ToString("f2") + "),Q=Q+(" + temp100.ToString("f2") + "),Uab=Uab+(" + (temp100 / 2).ToString("f2") + "),Iflc=Iflc+(" + temp100.ToString("f2") + ")," +
                    " KD=KD+(" + (temp100 / 5).ToString("f2") + "),ZS=ZS+(" + temp10.ToString("f2") + ")" +
                    " where DeviceId=5";
                BaseDao.execute(sql);

                //SBZ
                tempSBZ = RDNum1 / 20000.0;

                tempCQ = RDNum2 / 20000.0;
                tempJSJ = RDNum2 / 10000.0;

                sql = "update t_d_river set sbz=sbz+" + tempSBZ.ToString("f3") + ",xcqc=xcqc+" + tempCQ.ToString("f3") + ",xcjsj=xcjsj+" + tempJSJ.ToString("f3");

                tempCQ = RDNum3 / 20000.0;
                tempJSJ = RDNum3 / 10000.0;

                sql += ",jcqc=jcqc+" + tempCQ.ToString("f3") + ",jcjsj=jcjsj+" + tempJSJ.ToString("f3");
                BaseDao.execute(sql);

                //把数据库里面的票据等取出来赋值
                //string sql = "SELECT DATEDIFF(MINUTE, updateTime, GETDATE()) as diffTime,acc_Token,jsapi_ticket,noncestr,UNIXTime  from WxToken where WxTokenId=1";
                //List<Dictionary<string, object>> list = basedao.GetList(sql, new string[] { "diffTime", "acc_Token", "jsapi_ticket", "noncestr", "UNIXTime" });


                //
                Thread.Sleep(1000);
            }



        }
    }
}